---
header-includes:
   - \maxdeadcycles=200
output: pdf_document

---
```{r, echo = FALSE, include = FALSE}
setwd("/Users/mgdost/Dropbox/race_survey/psq materials/data and replication")
library(haven)
library(survey)
## load data
# 2017 data set
d17 <- read.csv("cleaned_datasets/harris_dec2017.csv")
# 2019 data set
d19 <- read.csv("cleaned_datasets/harris_feb2019.csv")

#### 2019 dataset prep
# subsetting datasets to remove obs with missing values 
vars <- c("stay_leave","male","BlackNH","Hispanic","OthNH","age","Age_30to49","Age_50to64","Age_65plus","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrLib","LeanLib","LeanConserv","StrConserv","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem","RECORD","weight")
wave2019 <- d19[vars]
wave2019 <- na.omit(wave2019)
# subsetting datasets
wave2019_St_Lv <- subset(wave2019, stay_leave==1 | stay_leave==2)
wave2019_St_Lv$stay <- 1
wave2019_St_Lv$stay[wave2019_St_Lv$stay_leave==2] <- 0
wave2019_Jo_Nv <- subset(wave2019, stay_leave==3 | stay_leave==4)
wave2019_Jo_Nv$never <- 1
wave2019_Jo_Nv$never[wave2019_Jo_Nv$stay_leave==3] <- 0
# setting up survey designs 
dsgn.stay <- svydesign(ids = ~RECORD, weights = ~weight, data = wave2019_St_Lv) 
dsgn.never <- svydesign(ids = ~RECORD, weights = ~weight, data = wave2019_Jo_Nv) 


#### 2017 dataset prep
## subsetting datasets to remove obs with missing values 
vars <- c("stay_leave","male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrLib","LeanLib","LeanConserv","StrConserv","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem","RESPID","weight")
wave2017 <- d17[vars]
wave2017 <- na.omit(wave2017)
# subsetting datasets 
wave2017_St_Lv <- subset(wave2017, stay_leave==1 | stay_leave==2)
wave2017_St_Lv$stay <- 1
wave2017_St_Lv$stay[wave2017_St_Lv$stay_leave==2] <- 0
wave2017_Jo_Nv <- subset(wave2017, stay_leave==3 | stay_leave==4)
wave2017_Jo_Nv$never <- 1
wave2017_Jo_Nv$never[wave2017_Jo_Nv$stay_leave==3] <- 0
# setting up survey designs 
dsgn.stay17 <- svydesign(ids = ~RESPID, weights = ~weight, data = wave2017_St_Lv) 
dsgn.never17 <- svydesign(ids = ~RESPID, weights = ~weight, data = wave2017_Jo_Nv) 

###### MODELS ######
# 2019 models #
mods <- rep(list(list()), 6)
for(i in 1){
  ## model 1: stay/leave: demographics + ideology ##
  formula.1 <- paste("stay","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrConserv","LeanConserv","LeanLib","StrLib"), collapse = "+"))
    ## model 2: stay/leave: demographics + party ID ##
  formula.2 <- paste("stay","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem"), collapse = "+"))
  ## model 3: stay/leave: demographics + ideology + party ID ##
  formula.3 <- paste("stay","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrConserv","LeanConserv","LeanLib","StrLib","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem"), collapse = "+"))

  ## model 4: join/never: demographics + ideology ##
  formula.4 <- paste("never","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrConserv","LeanConserv","LeanLib","StrLib"), collapse = "+"))
    ## model 5: join/never: demographics + party ID ##
  formula.5 <- paste("never","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem"), collapse = "+"))
    ## model 6: join/never: demographics + ideology + party ID ##
  formula.6 <- paste("never","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrConserv","LeanConserv","LeanLib","StrLib","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem"), collapse = "+"))
  ## running and saving out models ##
  j = (i-1)
  mods[[j+1]] <- svyglm(formula.1, design = dsgn.stay, family=quasibinomial())
  mods[[j+1]]$aic <- AIC(mods[[j+1]])[2]
  mods[[j+2]] <- svyglm(formula.2, design = dsgn.stay, family=quasibinomial())
  mods[[j+2]]$aic <- AIC(mods[[j+2]])[2]
  mods[[j+3]] <- svyglm(formula.3, design = dsgn.stay, family=quasibinomial())
  mods[[j+3]]$aic <- AIC(mods[[j+3]])[2]
  mods[[j+4]] <- svyglm(formula.4, design = dsgn.never, family=quasibinomial())
  mods[[j+4]]$aic <- AIC(mods[[j+4]])[2]
  mods[[j+5]] <- svyglm(formula.5, design = dsgn.never, family=quasibinomial())
  mods[[j+5]]$aic <- AIC(mods[[j+5]])[2]
  mods[[j+6]] <- svyglm(formula.6, design = dsgn.never, family=quasibinomial())
  mods[[j+6]]$aic <- AIC(mods[[j+6]])[2]
  }

# 2017 models #
mods17 <- rep(list(list()), 6)
for(i in 1){
  ## model 1: stay/leave: demographics + ideology ##
  formula.1 <- paste("stay","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrConserv","LeanConserv","LeanLib","StrLib"), collapse = "+"))
    ## model 2: stay/leave: demographics + party ID ##
  formula.2 <- paste("stay","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem"), collapse = "+"))
  ## model 3: stay/leave: demographics + ideology + party ID ##
  formula.3 <- paste("stay","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrConserv","LeanConserv","LeanLib","StrLib","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem"), collapse = "+"))

  ## model 4: join/never: demographics + ideology ##
  formula.4 <- paste("never","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrConserv","LeanConserv","LeanLib","StrLib"), collapse = "+"))
    ## model 5: join/never: demographics + party ID ##
  formula.5 <- paste("never","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem"), collapse = "+"))
    ## model 6: join/never: demographics + ideology + party ID ##
  formula.6 <- paste("never","~",paste(c("male","BlackNH","Hispanic","OthNH","age","Educ_SomeColl","Educ_CollGrad","Educ_PostGrad","Inc_35to75k","Inc_75to125k","Inc_125kplus","StrConserv","LeanConserv","LeanLib","StrLib","StrRep","NotStrRep","LeanRep","LeanDem","NotStrDem","StrDem"), collapse = "+"))
  ## running and saving out models ##
  j = (i-1)
  mods17[[j+1]] <- svyglm(formula.1, design = dsgn.stay17, family=quasibinomial())
  mods17[[j+1]]$aic <- AIC(mods17[[j+1]])[2]
  mods17[[j+2]] <- svyglm(formula.2, design = dsgn.stay17, family=quasibinomial())
  mods17[[j+2]]$aic <- AIC(mods17[[j+2]])[2]
  mods17[[j+3]] <- svyglm(formula.3, design = dsgn.stay17, family=quasibinomial())
  mods17[[j+3]]$aic <- AIC(mods17[[j+3]])[2]
  mods17[[j+4]] <- svyglm(formula.4, design = dsgn.never17, family=quasibinomial())
  mods17[[j+4]]$aic <- AIC(mods17[[j+4]])[2]
  mods17[[j+5]] <- svyglm(formula.5, design = dsgn.never17, family=quasibinomial())
  mods17[[j+5]]$aic <- AIC(mods17[[j+5]])[2]
  mods17[[j+6]] <- svyglm(formula.6, design = dsgn.never17, family=quasibinomial())
  mods17[[j+6]]$aic <- AIC(mods17[[j+6]])[2]
  }

### printing out model results ###
library(stargazer)

covarlabs <- c("Male","Black, non-Hisp","Hispanic","Other race, non-Hisp","Age","Educ: some college","Educ: coll grad","Educ: postgrad","Income: \\$35-75k","Income: \\$75-125k","Income: \\$125k+","Strong conserv","Lean conserv","Lean liberal","Strong liberal","Strong Repub","Not strong Rep","Lean Rep","Lean Dem","Not strong Dem","Strong Democrat","(Intercept)")
             
notes19 <- c("Data from 2019 survey. Models specified using logistic regression.","All variables are dummy variables, except age. Reference category for sex is female;","for race/ethnicity, white, non-Hispanic; for education, high school or less; for income,","<\\$35k; for political ideology, moderate; for party identification, pure Independent.")
notes17 <- c("Data from 2017 survey. Models specified using logistic regression.","All variables are dummy variables, except age. Reference category for sex is female;","for race/ethnicity, white, non-Hispanic; for education, high school or less; for income,","<\\$35k; for political ideology, moderate; for party identification, pure Independent.")
```

```{r, echo = FALSE, include = TRUE, results = 'asis'}
i = 1
j = (i-1)
cat(sub('^.+\\caption.+$','',stargazer(mods[[j+1]],mods[[j+2]],mods[[j+3]],mods[[j+4]],mods[[j+5]],mods[[j+6]], dep.var.labels = c("--- Republican: Remain Loyal vs. Switch ---","--- Democrat: Remain Loyal vs. Switch ---"), dep.var.caption = "", keep.stat = c("n","aic"), header = FALSE, type = 'latex', font.size = "scriptsize", title = "", no.space = T, covariate.labels = covarlabs, notes = notes19, notes.align = "l")), sep='\n')
#Predicting Staying with Trump (vs. Leaving) OR being Never Trump (vs. Joining), 2019

cat(sub('^.+\\caption.+$','',stargazer(mods17[[j+1]],mods17[[j+2]],mods17[[j+3]],mods17[[j+4]],mods17[[j+5]],mods17[[j+6]], dep.var.labels = c("--- Republican: Remain Loyal vs. Switch ---","--- Democrat: Remain Loyal vs. Switch ---"), dep.var.caption = "", keep.stat = c("n","aic"), header = FALSE, type = 'latex', font.size = "scriptsize", title = "", no.space = T, covariate.labels = covarlabs, notes = notes17, notes.align = "l")), sep='\n')
#Predicting Staying with Trump (vs. Leaving) OR being Never Trump (vs. Joining), 2017



```
